package com.sen.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.reggie.common.CustomException;
import com.sen.reggie.dto.DishDto;
import com.sen.reggie.entity.Dish;
import com.sen.reggie.entity.DishFlavor;
import com.sen.reggie.mapper.DishMapper;
import com.sen.reggie.service.DishFlavorService;
import com.sen.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Zs
 * @date:2022/6/2 18:37
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private  DishService dishService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    public void  saveWithFlavor (DishDto dishDto){
        //保存菜品的基本信息到dish表中
        this.save(dishDto);

        Long dishId=dishDto.getId();//菜品ID
        //菜品口味
        List<DishFlavor> flavors=dishDto.getFlavors();
        flavors.stream().map((item)->{
                  item.setDishId(dishId);
                  return item;
        }).collect(Collectors.toList());
        //菜品口味
        dishFlavorService.saveBatch(dishDto.getFlavors());

    }

    /**
     * 根据ID查询相当应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表中查
         Dish dish=this.getById(id);

         DishDto dishDto= new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从Dish_flavor表中查询
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor>flavors =dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
       //更新dish表基本信息
        this.updateById(dishDto);
       //清理当前菜品对应的口味数据--dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
       //添加当前提交过来的口味数据--dish_flavor表的insert操作
        List<DishFlavor> flavors=dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return  item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);


    }

    @Override
    public void deleteWithFlavor(List<Long> ids) {
        //创建构造器，只有在停售状态下的菜品才能够删除
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        //创建查询条件 的SQL
        queryWrapper.in(Dish::getId,ids);
        //创建模糊查询
        queryWrapper.like(Dish::getStatus,1);
        long count = dishService.count(queryWrapper);
        if (count>0) {
            throw  new CustomException("菜品正在出售中，删除失败");
        }
          this.removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lambdaQueryWrapper);
    }
}
